package com.leha.forum.lehare.controller;

import com.leha.forum.lehare.entity.Level;
import com.leha.forum.lehare.service.LevelService;
import com.leha.forum.lehare.response.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/levels")
@RequiredArgsConstructor
public class LevelController {

    @Autowired
    private LevelService levelService;

    @PostMapping
    public Result<Void> saveLevel(@RequestBody Level level) {
        levelService.createLevel(level);
        return Result.success();
    }

    @GetMapping("/{levelId}")
    public Result<Level> getLevel(@PathVariable Integer levelId) {
        return Result.success(levelService.getUserLevelInfo(levelId));
    }

    @GetMapping
    public Result<List<Level>> getAllLevels() {
        return Result.success(levelService.getAllLevels());
    }
    @PostMapping("/user/{userId}/experience")
    public Result<Void> addExperience(@PathVariable Integer userId,
                                      @RequestParam int points) {
        levelService.addUserExperience(userId, points);
        return Result.success();
    }

    @GetMapping("/user/{userId}/current")
    public Result<Level> getCurrentLevel(@PathVariable Integer userId) {
        return Result.success(levelService.getCurrentUserLevel(userId));
    }



    @PutMapping("/{levelId}")
    public Result updateLevel(@PathVariable Integer levelId,
                              @RequestBody Level level) {
        level.setLevelId(levelId);
        Level updatedLevel = levelService.updateLevel(level);
        return Result.success("等级更新成功", updatedLevel);
    }

    @DeleteMapping("/{levelId}")
    public Result deleteLevel(@PathVariable Integer levelId) {
        levelService.deleteLevel(levelId);
        return Result.success("等级删除成功");
    }

    @GetMapping("/by-experience")
    public Result getLevelsByExperience(@RequestParam Integer experience) {
        List<Level> levels = levelService.getLevelsByExperience(experience);
        return Result.success(levels);
    }

   /* @PutMapping("/{levelId}/promotion")
    public Result setPromotionConditions(@PathVariable Integer levelId,
                                         @RequestParam Integer postCount,
                                         @RequestParam Integer replyCount,
                                         @RequestParam Integer essenceCount,
                                         @RequestParam Integer minExperience) {
        levelService.setPromotionConditions(levelId, postCount, replyCount, essenceCount, minExperience);
        return Result.success("等级晋升条件设置成功");
    }*/
}